package com.baidu.infinity.ui.view

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.Typeface
import android.util.AttributeSet
import android.view.Gravity
import androidx.appcompat.widget.AppCompatTextView
import com.baidu.infinity.model.IconModel
import com.baidu.infinity.ui.util.state.IconState

class IconTextView(
    context: Context,attrs: AttributeSet?= null
): AppCompatTextView(context) {
    private var mIconModel: IconModel ?= null
    var onClick: ()-> Unit = {}
    init {
        typeface = Typeface.createFromAsset(context.assets,"iconfont.ttf")
        gravity = Gravity.CENTER
        setTextColor(Color.WHITE)
        setOnClickListener{
            mIconModel?.let {
                if(it.state == IconState.NORMAL){
                    it.state = IconState.SELECTED
                    setBackgroundColor(resources.getColor(it.selectedColor,null))
                }else{
                    it.state = IconState.NORMAL
                    setBackgroundColor(resources.getColor(it.normalColor,null))
                }
            }
            val intent = Intent()
            intent.action = "icon_click"
            intent.setPackage("com.baidu.infinity")
            intent.putExtra("model",mIconModel)
            context.sendBroadcast(intent)
            onClick()

        }
    }
    fun setIconModel(model: IconModel){
        mIconModel = model
        setText(model.iconName)
        setBackgroundColor(resources.getColor(model.normalColor,null))
    }
    fun cancelSelectedColor(){
        setBackgroundColor(resources.getColor(mIconModel!!.normalColor,null))
        mIconModel?.state = IconState.NORMAL

    }
}